#include "HashFunction.hpp"

HashFunction::HashFunction(unsigned int hashLength) : value(hashLength) {
	clear();
}

void HashFunction::clear() {
	md5_init(&state);
}

void HashFunction::hash(const std::string &text) {
	md5_append(&state, (const md5_byte_t*)text.c_str(), text.size());
}

void HashFunction::saveState() {
	savedState = state;
}

void HashFunction::loadState() {
	state = savedState;
}

void HashFunction::finish() {
	md5_finish(&state, value.getDataPtr());
}

const HashValue& HashFunction::getHash() const {
	return value;
}

HashValue HashFunction::calculateHash(unsigned int length, const std::string &text) {
	HashFunction f(length);
	f.hash(text);
	f.finish();
	return f.getHash();
}

